package com.example.firstdemo.controller;

import org.springframework.web.bind.annotation.RestController;

import com.example.firstdemo.Response;
import com.example.firstdemo.dao.Student;
import com.example.firstdemo.dto.StudentDTO;
import com.example.firstdemo.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;



@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping("/student/{id}")
        
    public Response<StudentDTO> getStudentById(@PathVariable long id){
        return Response.newSuccess(studentService.getStudentById(id));
    }

    @PostMapping("/student")    
    public Response<Long> addNewStudent(@RequestBody StudentDTO studentDTO){
        return Response.newSuccess(studentService.addNewStudent(studentDTO));
    }

    @DeleteMapping("/student/{id}")
    public void deleteStudentById(@PathVariable long id){
        studentService.deleteStudentById(id);
    }
    @PutMapping("/student/{id}")
    public Response<StudentDTO> updateStudent(
            @PathVariable long id,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String email

    ){
        return Response.newSuccess(studentService.updateStudentById(id,name,email));
    }
}
